<?php
App::uses('AppController', 'Controller');
/**
 * Rooms Controller
 *
 * @property Room $Room
 * @property PaginatorComponent $Paginator
 * @property SessionComponent $Session
 */
class RoomsController extends AppController {

    
/**
 * Components
 *
 * @var array
 */
	public $components = array('Paginator', 'Session');
        public $uses = array('Room', 'Booking', );
/**
 * index method
 *
 * @return void
 */
	public function index() {
		$this->Room->recursive = 0;
		$this->set('rooms', $this->Paginator->paginate());
	}

/**
 * view method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function view($id = null) {
		if (!$this->Room->exists($id)) {
			throw new NotFoundException(__('Invalid room'));
		}
		$options = array('conditions' => array('Room.' . $this->Room->primaryKey => $id));
		$this->set('room', $this->Room->find('first', $options));
	}

/**
 * add method
 *
 * @return void
 */
	public function add() {
		if ($this->request->is('post')) {
			$this->Room->create();
			if ($this->Room->save($this->request->data)) {
				$this->Session->setFlash(__('The room has been saved.'));
				return $this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The room could not be saved. Please, try again.'));
			}
		}
		$features = $this->Room->Feature->find('list');
		$this->set(compact('features'));
	}

/**
 * edit method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function edit($id = null) {
		if (!$this->Room->exists($id)) {
			throw new NotFoundException(__('Invalid room'));
		}
		if ($this->request->is(array('post', 'put'))) {
			if ($this->Room->save($this->request->data)) {
				$this->Session->setFlash(__('The room has been saved.'));
				return $this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The room could not be saved. Please, try again.'));
			}
		} else {
			$options = array('conditions' => array('Room.' . $this->Room->primaryKey => $id));
			$this->request->data = $this->Room->find('first', $options);
		}
		$features = $this->Room->Feature->find('list');
		$this->set(compact('features'));
	}

/**
 * delete method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function delete($id = null) {
		$this->Room->id = $id;
		if (!$this->Room->exists()) {
			throw new NotFoundException(__('Invalid room'));
		}
		$this->request->onlyAllow('post', 'delete');
		if ($this->Room->delete()) {
			$this->Session->setFlash(__('The room has been deleted.'));
		} else {
			$this->Session->setFlash(__('The room could not be deleted. Please, try again.'));
		}
		return $this->redirect(array('action' => 'index'));
	}

        public function matchingRooms(){
            //This must be accesible + ajax. This should be triggered and handled by ajax.
            //For example see the feed in BookingsController
            debug ($this->params['url']);
            //Get a list of all the rooms with an array of the features
            $rooms = $this->Room->find('all', array('conditions' => array('seats >=' => $this->params['url']['seats'])));                
            //Get all Bookings for this day. If the start or End of a booking is in our meeting time the room is not free.
            //Conditions aufbauen
            $conditions = array();
            $bookings = $this->Room->Booking->find('all', array('conditions' => $conditions));     
            
            
            $data = array();
           // debug($rooms);
            foreach ($rooms as $room) {
                $features = ' ';
                foreach($room['Feature'] as $feature){
                    $features=$features.' - '.$feature['name'].'<br>';
                }
                array_push($data, array('<input type="radio" name="room_id" id="room_id" value="'.$room['Room']['id'].'">   '.$room['Room']['name'], $room['Room']['description'],$features));
            }                       
            $roomsTable['data'] = $data;
            Configure::write('debug', 0);
            $this->autoRender = false;
            $this->autoLayout = false;
            $this->header('Content-Type: application/json');
            echo json_encode($roomsTable);  
        }
        

}
